/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.VirtualMachineResources;
import it.polimi.games.epkb.entityClasses.VirtualMachineResourcesPK;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.VirtualMachineInst;
import it.polimi.games.epkb.entityClasses.Component;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class VirtualMachineResourcesJpaController implements Serializable {

    public VirtualMachineResourcesJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }
    public VirtualMachineResourcesJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(VirtualMachineResources virtualMachineResources) throws PreexistingEntityException, Exception {
        if (virtualMachineResources.getVirtualMachineResourcesPK() == null) {
            virtualMachineResources.setVirtualMachineResourcesPK(new VirtualMachineResourcesPK());
        }
        virtualMachineResources.getVirtualMachineResourcesPK().setIdComponent(virtualMachineResources.getComponent().getId());
        virtualMachineResources.getVirtualMachineResourcesPK().setIdVMInstance(virtualMachineResources.getVirtualMachineInst().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            VirtualMachineInst virtualMachineInst = virtualMachineResources.getVirtualMachineInst();
            if (virtualMachineInst != null) {
                virtualMachineInst = em.getReference(virtualMachineInst.getClass(), virtualMachineInst.getId());
                virtualMachineResources.setVirtualMachineInst(virtualMachineInst);
            }
            Component component = virtualMachineResources.getComponent();
            if (component != null) {
                component = em.getReference(component.getClass(), component.getId());
                virtualMachineResources.setComponent(component);
            }
            em.persist(virtualMachineResources);
            if (virtualMachineInst != null) {
                virtualMachineInst.getVirtualMachineResourcesCollection().add(virtualMachineResources);
                virtualMachineInst = em.merge(virtualMachineInst);
            }
            if (component != null) {
                component.getVirtualMachineResourcesCollection().add(virtualMachineResources);
                component = em.merge(component);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findVirtualMachineResources(virtualMachineResources.getVirtualMachineResourcesPK()) != null) {
                throw new PreexistingEntityException("VirtualMachineResources " + virtualMachineResources + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(VirtualMachineResources virtualMachineResources) throws NonexistentEntityException, Exception {
        virtualMachineResources.getVirtualMachineResourcesPK().setIdComponent(virtualMachineResources.getComponent().getId());
        virtualMachineResources.getVirtualMachineResourcesPK().setIdVMInstance(virtualMachineResources.getVirtualMachineInst().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            VirtualMachineResources persistentVirtualMachineResources = em.find(VirtualMachineResources.class, virtualMachineResources.getVirtualMachineResourcesPK());
            VirtualMachineInst virtualMachineInstOld = persistentVirtualMachineResources.getVirtualMachineInst();
            VirtualMachineInst virtualMachineInstNew = virtualMachineResources.getVirtualMachineInst();
            Component componentOld = persistentVirtualMachineResources.getComponent();
            Component componentNew = virtualMachineResources.getComponent();
            if (virtualMachineInstNew != null) {
                virtualMachineInstNew = em.getReference(virtualMachineInstNew.getClass(), virtualMachineInstNew.getId());
                virtualMachineResources.setVirtualMachineInst(virtualMachineInstNew);
            }
            if (componentNew != null) {
                componentNew = em.getReference(componentNew.getClass(), componentNew.getId());
                virtualMachineResources.setComponent(componentNew);
            }
            virtualMachineResources = em.merge(virtualMachineResources);
            if (virtualMachineInstOld != null && !virtualMachineInstOld.equals(virtualMachineInstNew)) {
                virtualMachineInstOld.getVirtualMachineResourcesCollection().remove(virtualMachineResources);
                virtualMachineInstOld = em.merge(virtualMachineInstOld);
            }
            if (virtualMachineInstNew != null && !virtualMachineInstNew.equals(virtualMachineInstOld)) {
                virtualMachineInstNew.getVirtualMachineResourcesCollection().add(virtualMachineResources);
                virtualMachineInstNew = em.merge(virtualMachineInstNew);
            }
            if (componentOld != null && !componentOld.equals(componentNew)) {
                componentOld.getVirtualMachineResourcesCollection().remove(virtualMachineResources);
                componentOld = em.merge(componentOld);
            }
            if (componentNew != null && !componentNew.equals(componentOld)) {
                componentNew.getVirtualMachineResourcesCollection().add(virtualMachineResources);
                componentNew = em.merge(componentNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                VirtualMachineResourcesPK id = virtualMachineResources.getVirtualMachineResourcesPK();
                if (findVirtualMachineResources(id) == null) {
                    throw new NonexistentEntityException("The virtualMachineResources with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(VirtualMachineResourcesPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            VirtualMachineResources virtualMachineResources;
            try {
                virtualMachineResources = em.getReference(VirtualMachineResources.class, id);
                virtualMachineResources.getVirtualMachineResourcesPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The virtualMachineResources with id " + id + " no longer exists.", enfe);
            }
            VirtualMachineInst virtualMachineInst = virtualMachineResources.getVirtualMachineInst();
            if (virtualMachineInst != null) {
                virtualMachineInst.getVirtualMachineResourcesCollection().remove(virtualMachineResources);
                virtualMachineInst = em.merge(virtualMachineInst);
            }
            Component component = virtualMachineResources.getComponent();
            if (component != null) {
                component.getVirtualMachineResourcesCollection().remove(virtualMachineResources);
                component = em.merge(component);
            }
            em.remove(virtualMachineResources);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<VirtualMachineResources> findVirtualMachineResourcesEntities() {
        return findVirtualMachineResourcesEntities(true, -1, -1);
    }

    public List<VirtualMachineResources> findVirtualMachineResourcesEntities(int maxResults, int firstResult) {
        return findVirtualMachineResourcesEntities(false, maxResults, firstResult);
    }

    private List<VirtualMachineResources> findVirtualMachineResourcesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(VirtualMachineResources.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public VirtualMachineResources findVirtualMachineResources(VirtualMachineResourcesPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(VirtualMachineResources.class, id);
        } finally {
            em.close();
        }
    }

    public int getVirtualMachineResourcesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<VirtualMachineResources> rt = cq.from(VirtualMachineResources.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
